package io.modifier.basic.compose.widget.comm

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.wquick.base.R
import io.modifier.basic.compose.theme.AppTheme
import io.modifier.basic.compose.theme.color
import io.modifier.basic.compose.theme.size
import io.modifier.basic.compose.theme.appColors
import io.modifier.basic.compose.theme.appTextStyle
import io.modifier.basic.compose.theme.weight
import io.modifier.basic.emo.ui.core.modifier.throttleClick

data class MenuPopItem(
    val icon: Int,
    val title: String,
    val height: Number = 44,
    val tintColor: Color? = null,
    val onClick: () -> Unit
) {
    companion object {
        fun genDivider(height: Number = 10): MenuPopItem {
            return MenuPopItem(0, "", height = height) {
            }
        }
    }
}

@Composable
fun MenuPop(menus: List<MenuPopItem>) {
    Box(
        Modifier
            .clip(RoundedCornerShape(13.dp))
            .background(MaterialTheme.appColors.bgColor)
    ) {
        LazyColumn(Modifier) {
            items(menus) {
                Column(
                    Modifier
                        .align(Alignment.Center)
                        .width(IntrinsicSize.Min)
                        .defaultMinSize(minWidth = 160.dp)
                ) {

                    val justDivider = it.icon <= 0 && it.title.isBlank()

                    if (justDivider.not())
                        Row(
                            Modifier
                                .wrapContentWidth()
                                .height((it.height).toDouble().dp)
                                .throttleClick { it.onClick.invoke() }
                                .padding(horizontal = 16.dp),
                            verticalAlignment = Alignment.CenterVertically,
                            horizontalArrangement = Arrangement.Center
                        ) {

                            if (it.icon > 0) {
                                Image(
                                    painter = painterResource(it.icon),
                                    contentDescription = null,
                                    modifier = Modifier.size(24.dp),
                                    colorFilter = it.tintColor?.let { ColorFilter.tint(it) }
                                )

                                H(20)
                            }

                            Text(
                                it.title,
                                style = MaterialTheme.appTextStyle.default.size(17).weight(400)
                                    .color(it.tintColor ?: MaterialTheme.appColors.txtColor)
                            )
                        }

                    Box(
                        Modifier
                            .fillMaxWidth()
                            .height(if (justDivider.not()) 1.dp else (it.height).toDouble().dp)
                            .background(MaterialTheme.appColors.dividerColor)
                    )

                }
            }
        }
    }
}

@Preview(backgroundColor = 0xFF121212, showBackground = true)
@Composable
private fun Preview() {
    AppTheme {
        MenuPop(
            listOf(
                MenuPopItem(0, "Nickname") {
                },
                MenuPopItem(R.drawable.compose_views_refresh_layout_loading, "Nickname") {
                },
                MenuPopItem(R.drawable.compose_views_refresh_layout_loading, "Nickname") {
                },

                MenuPopItem.genDivider(10),

                MenuPopItem(R.drawable.compose_views_refresh_layout_loading, "Delete", tintColor = Color.Red) {
                },
            )
        )
    }
}